<?php

require '../vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

function postfeishu($headers, $body, $curl)
{
    return;
    $client = new Client();
    $request = new Request('POST', $curl, $headers, $body);

    $response = $client->sendAsync($request);
    $res = $response->wait();
    // // $res = $client->sendAsync($request)->wait();
    return $res;
    // $rbody = json_decode($res->getBody());
    // return $rbody;
}

function gettenant()
{
    $headers = [
        'Content-Type' => 'application/json',
    ];
    $body = '{"app_id": "cli_a0654c282dfa900c", "app_secret": "WxBjeftzSigaWSxeXwu7IdwQEdOvhp4w"}';
    $curl = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal';
    $res = postfeishu($headers, $body, $curl);
    $rbody = json_decode($res->getBody());
    $tenant_access_token = $rbody->tenant_access_token;
    return $tenant_access_token;
}

function sendtofeishu($receive_id, $message)
{
    return;
    if ($receive_id == 1 or $receive_id == '1') {
        return;
    }
    if ($receive_id == null) {
        $receive_id = 'f7a2eedf';
        $message = '有人飞书ID为空';
    }
    $tenant_access_token = gettenant();
    $headers = [
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer ' . $tenant_access_token,
    ];

    $content = [
        'text' => $message,
    ];
    $data = [
        'content' => json_encode($content),
        'receive_id' => $receive_id,
        'msg_type' => 'text',
    ];
    $j_data = json_encode($data);
    // $body = '{"content": "{\"text\":\"test content\"}","receive_id": "f7a2eedf", "msg_type": "text"}';
    $curl = 'https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=user_id';
    postfeishu($headers, $j_data, $curl);
}
